<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="keywords" content="智慧楼宇"/>
  <meta name="description" content="智慧楼宇"/>
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title></title>
  <link rel="shortcut icon" href="favicon_blue.ico"/>
  <link rel="stylesheet" href="styles/libs/bootstrap_3.3.7.css">
  <link rel="stylesheet" href="styles/libs/font.css">
  <link rel="stylesheet" href="styles/app/default/default.min.css">
  <script src="scripts/libs/vue.min.js" charset="utf-8"></script>
</head>

<style>
  .login-wrap{
    position: fixed;
    left:50%;
    top:50%;
    transform: translate(-50%,-50%);
    width: 424px;
    z-index: 1000;
  }
  .login-wrap .content{
    padding: 66px 0 24px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 2px 2px 6px rgba(155,155,155,.75);
    text-align: center;
  }
  .login-wrap .title{
    font-size: 20px;
    color:#333;
    margin-bottom: 6px;
  }
  .login-wrap .mes{
    color:#666;
    margin-bottom: 30px;
  }
  .login-wrap .bottom{
    height: 50px;
    line-height: 50px;
    text-align: center;
    background-color: #eaeaea;
    border-bottom-left-radius: 4px;
    border-bottom-right-radius: 4px;
    box-shadow: 2px 2px 6px rgba(155,155,155,.75);
  }
</style>

<body>
  <section id="ddLogin">
    <div class="login-wrap" v-if="isShow">
      <div class="content">
        <p class="title"></p>
        <p class="mes">请联系公司钉钉管理员为你创建账号。</p>
        <a href="javascript:;" @click="goTologin">我是管理员</a>
      </div>
    </div>
  </section>

  <script src="https://g.alicdn.com/dingding/dingtalk-jsapi/2.0.57/dingtalk.open.js" charset="utf-8"></script>
  <script src="scripts/libs/jquery_1.9.1.js" charset="utf-8"></script>
  <script src="scripts/libs/cookie_2.1.3.js"  charset="utf-8"></script>
  <script src="scripts/app/toast_1.0.0.js" charset="utf-8"></script>
  <script src="scripts/app/http.js" charset="utf-8"></script>
  <script src="scripts/app/storage.js" charset="utf-8"></script>

  <script type="text/javascript">
    ;(function(){
      new Vue({
        el:'#ddLogin',
        data:{isShow:false},
        mounted(){
          this.getCorpId()
        },
        methods:{
          // 获取钉钉内嵌的第三方企业
          getCorpId(){
            var self = this;
            var url = window.location.href
            var tenantCode;

            if(url.indexOf('normalUser') > -1){
              tenantCode = window.localStorage.getItem('tenantCode');
            }else{
              tenantCode = url.substring(url.indexOf('?') + 4,url.length);
              window.localStorage.setItem('tenantCode',tenantCode);
            }

            hlhttp.get({
              url:'tenant/corp/corp_id',
              data:{'tc':tenantCode,'corp_type':'DD'},
              success:function(data){
                if(data.corpId) self.getAuthCode(data.corpId,tenantCode)
              },
              error:function(){
                self.isShow = true;
              }
            })
          },
          // 获取授权码
          getAuthCode(corpId,tenantCode){
            var self = this;
            dd.runtime.permission.requestAuthCode({
              corpId: corpId,
              onSuccess: function(data) {
                self.authLogin(tenantCode,data.code)
              }
            })
          },
          // 根据授权code联合登录
          authLogin(tenantCode,authCode){
            hlhttp.post({
              url:'corp/login',
              data:{'tc':tenantCode,'auth_code':authCode,'corp_type':'DD'},
              success:function(data){
                var userMes = {
                  id: data.id,
                  name: data.name,
                  account: data.account,
                  phone: data.phone,
                  email: data.email,
                  tenantType: data.tenantType,
                  tenantId:data.tenantId,
                  isAdmin:data.isAdmin,
                  ico: data.cstmFaviconUrl || '',
                  logo: data.cstmLogoUrl || '',
                  title: data.cstmWebTitle || '',
                  preSysCode: data.preSysCode || '',
                  cstmHomepage: data.cstmHomepage || '',
                  oauthType:data.oauthType || ''
                }
                Cookies.set("token", data.token, {expires: 30})
                window.localStorage.setItem('userMes', JSON.stringify(userMes))
                window.sessionStorage.setItem('names', data.name)

                // 清除原来所用的存储字段
                var originalArr = [
                  'names',
                  'navPos',
                  'projectName',
                  'unitData',
                  'contractNav1'
                ]
                hlStorage.lsClearMultipleKey(originalArr)

                var pathname = window.location.pathname.replace('ddlogin', 'index')
                window.location.href = './#/home';
              }
            })
          },
          goTologin:function(){
            window.location.href = 'ddAdminLogin.html'
          }
        }
      })

    }())
  </script>



</body>
</html>
